﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diplomova_prace
{
    /// <summary>
    /// Třida pro renderování polynomů, využívá se v metodách pro aproximaci funkce
    /// Nutno předělat
    /// </summary>
    class ntice
    {
        public List<double> hodnoty = new List<double>();
        bool x;

        public ntice(bool x = false)
        {
            this.x = x;
        }
        public ntice(List<double> hodnoty,bool x)
        {
            this.hodnoty = new List<double>(hodnoty);
            this.x = x;
        }
        public void Add(double num)
        {
            hodnoty.Add(num);
        }
        public double Last()
        {
            return  hodnoty.Last();
        }
        public void Copy(ntice n)
        {
            this.hodnoty = new List<double>(n.hodnoty);
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            string ret = string.Empty;

            if (x)
            {
                for (int i = hodnoty.Count - 1; i > -1; i--)
                {
                    //hodnota
                    double prah = 0.000005;
                    double HorniCelaCast = Math.Ceiling(hodnoty[i]);
                    double DolniCelaCast = Math.Floor(hodnoty[i]);
                    if (HorniCelaCast != hodnoty[i] || DolniCelaCast != hodnoty[i])
                    {
                        double rozdilH = Math.Abs(Math.Abs(HorniCelaCast) - Math.Abs(hodnoty[i]));
                        double rozdilL = Math.Abs(Math.Abs(DolniCelaCast) - Math.Abs(hodnoty[i]));
                        if (rozdilH < prah)
                            hodnoty[i] = HorniCelaCast;
                        if (rozdilL < prah)
                            hodnoty[i] = DolniCelaCast;
                    }


                    if (hodnoty[i] > 0)
                    {
                        if (hodnoty[i] == 1 && i != 0)
                            sb.Append(string.Empty);
                        else if (i != hodnoty.Count - 1) //clen neni prvni v polynomu, spojeni operatorem mezi cleny v polynomu
                            sb.Append("<mo>+</mo><mn>" + hodnoty[i].ToString() + "</mn>");
                        else
                            sb.Append("<mn>" + hodnoty[i] + "</mn>");
                    }
                    else if ( hodnoty[i] == 0 &&  i ==hodnoty.Count-1)
                        sb.Append("<mn>"+hodnoty[i] + "</mn>");
                    else
                        sb.Append("<mo>-</mo><mn>" + Math.Abs(hodnoty[i]) + "</mn>");
                    //exponent
                    if (i > 1)
                        sb.Append("<msup><mrow><mi>x</mi></mrow><mn>" + i + "</mn></msup>");
                    else if (i == 1)
                        sb.Append("<mi>x</mi>");

                }
            }
            else
            {
                for (int i = 0; i < hodnoty.Count; i++)
                {
                    if (hodnoty[i] >= 0)
                        if (i != 0)
                            sb.Append("<mo>+</mo><mn>" + hodnoty[i] + "</mn>");
                        else
                            sb.Append("<mn>" + hodnoty[i] + "</mn>");
                    else
                        sb.Append("<mo>-</mo><mn>" + Math.Abs(hodnoty[i]) + "</mn>");

                }

            }

            return sb.ToString();
        }
        
        public string ToStringResult()
        {
            StringBuilder sb = new StringBuilder();
            string ret = string.Empty;
            int counter = 0;
                for (int i = hodnoty.Count - 1; i > -1; i--)
                {
                    double prah = 0.0005;
                    double HorniCelaCast = Math.Ceiling(hodnoty[i]);
                    double DolniCelaCast = Math.Floor(hodnoty[i]);
                    if (HorniCelaCast != hodnoty[i] || DolniCelaCast != hodnoty[i])
                    {
                        double rozdilH = Math.Abs(Math.Abs(HorniCelaCast) - Math.Abs(hodnoty[i]));
                        double rozdilL = Math.Abs(Math.Abs(DolniCelaCast) - Math.Abs(hodnoty[i]));
                        if (rozdilH < prah)
                            hodnoty[i] = HorniCelaCast;
                        if (rozdilL < prah)
                            hodnoty[i] = DolniCelaCast;
                    }

                    if (hodnoty[i] == 0)
                    {
                        counter = 1;
                        continue;
                        
                    }
                    if (hodnoty[i] > 0)
                    {
                        if (hodnoty[i] == 1 && i != 0)
                        {
                            sb.Append(String.Empty);
                            counter = 1;
                        }
                        else if (hodnoty[i] == 1 && i != 0 && counter != 0)
                            sb.Append("<mo>+</mo>");

                        else if (i != hodnoty.Count - 1) //clen neni prvni v polynomu, spojeni operatorem mezi cleny v polynomu
                            sb.Append("<mo>+</mo><mn>" + hodnoty[i].ToString() + "</mn>");
                        else if (counter < 0)
                        {
                            counter = 0;
                            sb.Append("<mn>" + hodnoty[i].ToString() + "</mn>");
                        }
                        else
                            sb.Append("<mn>" + hodnoty[i] + "</mn>");
                    }
                    else
                    {
                        counter = -1;
                        if (hodnoty[i] == -1 && i != 0)
                            sb.Append("<mo>-</mo>");
                        else
                        sb.Append("<mo>-</mo><mn>" + Math.Abs(hodnoty[i]) + "</mn>");
                    }
                    //exponent
                    if (i > 1)
                        sb.Append("<msup><mrow><mi>x</mi></mrow><mn>" + i + "</mn></msup>");
                    else if (i == 1)
                        sb.Append("<mi>x</mi>");

                }
            
            return sb.ToString();
            }
        }
    class SeznamNtic
    {
       public List<ntice> list = new List<ntice>();

        public void Add(ntice d)
        {
            this.list.Add(d);
        }
        public void RemoveAtList(int num)
        {
            this.list.RemoveAt(num);
        }
        public void Copy(List<ntice> list)
        {
            this.list = new List<ntice>(list);
        }
      
    }
}
